import {CHANGETOPATH, LOGIN ,MENUINDEX} from "../../../main/store/mutation-types";
import store from '../../../main/store/index'
import appConfig from "../../../resources/config/app/app.json"
import anon from "../../../resources/config/app/anon.json"
import Cookies from 'cookies-js'
import { Message } from 'element-ui';
function getToken(){
    if (appConfig.saveTokenType == 2){
        return Cookies.get("authToken")
    }else if(appConfig.saveTokenType == 1){
        let loginTime = window.localStorage['loginTime'];
        let loginData = window.localStorage['loginData'];
        if(loginTime !== undefined && loginData !== undefined && loginTime !== null && loginData !==null ){
            let expires_in = JSON.parse(loginData)['expires_in'];
            let currentTime = new Date().getTime();
            if((currentTime - JSON.parse(loginTime)['loginTime'])/1000 + 300 < expires_in){           /*计算token是否超时，300是误差*/
                return window.localStorage['authToken']
            }else{
                debugger;
                window.localStorage.clear();
                return null;
            }
        }

    }else {
        return null;
    }
}

function isAuth(path){

    for(let key in anon){
        if (path===anon[key]){
            return false
        }
    }

    return true

}

//根据业务需要添加的方法
function hasPermission(path){
    debugger
    let menu = JSON.parse(window.localStorage['menu']);
    if(menu.length > 0){
        for(let i in menu){
            if(menu[i]['path'] === path){
                // window.localStorage.setItem('menuIndex',menu[i]['id']);
                store.commit(MENUINDEX,menu[i]['id']+'');
                return true;
            }
        }
    }
    return false;
}

exports.loginEach = function (router) {
    router.beforeEach((to, from, next) => {
        // if (to.path !== "/login") {
        if (isAuth(to.path)) {
            // 获取token
            let token = getToken();
            if (!token) {
                //无token踢到登录页面
                next({path: "/login"});
            }else {
                //菜单权限开关
                if(!appConfig.menuOpen){
                    //根据业务需要添加的判断
                    if(to.path == '/mainPage/welcome'||to.path == '/mainPage/upLoader'){
                        store.commit(MENUINDEX,'');
                        next();
                    }else if(to.path == '/'){
                        next({path: "/mainPage/welcome"});
                    }else if(hasPermission(to.path)){
                        next();
                    }else{
                        /*alert('没有权限访问该页面');*/
                        Message.success('没有权限访问该页面')
                        next(false);
                    }
                }else{
                    next();
                }
            }
/*            if (store.state.logined) {
                // 有登录状态即可正常访问页面
                //注意vuex的状态是存储在内存中的，所以每当刷新一次，其值为空，在这里使用的目的是在单页面访问时，减少频繁验证token的次数，当然每次请求后端其实都会验证token
                //next(store.state.toPath!=""?store.state.toPath:"/")
                next();
            } else {
                    // 获取token
                    let token = getToken();
                    if (!token) {
                        store.commit(CHANGETOPATH, to.path)
                        //无token踢到登录页面
                        next({path: "/login"});
                    } else {
                        //进行token认证
                        Axios.post("/validateToken").then(function (res) {

                                if (res.data.code == 200) {
                                    //认证成功
                                    store.commit(LOGIN)
                                    //next(store.state.toPath!=""?store.state.toPath:"/")
                                    next();
                                }else{
                                    store.commit(CHANGETOPATH, to.path)
                                    next({path: "/login"});
                                }
                            }
                        ).catch(function (error) {
                            store.commit(CHANGETOPATH, to.path)
                            next({path: "/login"});
                        })
                    }

            }*/


        } else {
            //直接请求login 默认跳到首页
            next();
        }
        // console.log(store.state.logined)
    })

}